package com.kolon.business.mOI_.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mOI_.controller.MActIdeaSuggestionViewController")
public class MActIdeaSuggestionViewController implements SimpleController {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/business/mOI_/mActIdeaSuggestionView.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// XML URL
		String url = MsgUtil.getMessage("xml.url");
		// 파라미터
		String seq = request.getParameter("seq");
		String eseq = request.getParameter("eseq");
		String page = request.getParameter("page");
		// 세션
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String userId = (String)userMap.get("userId");
		String userPw = (String)userMap.get("userPw");
		String cpyCD = (String)userMap.get("CompanyCD");
		// URI
		StringBuffer buffer = new StringBuffer();
		buffer.append(url); 
		buffer.append("/oi_suggestion.aspx?strGubun=ExecView&mode=6");
		buffer.append("&Seq=").append(seq);
		buffer.append("&ESeq=").append(eseq);
		buffer.append("&CompanyCD=").append(cpyCD);
		// 아이디어 제안 내용
		Document doc = ComUtil.getDocument(buffer.toString(), userId, userPw);
		Element element = doc.getRootElement();
		List xmlList = element.getChildren("BoardDetail");
		Iterator itXml = xmlList.iterator();
		Map map = null;
		// XML 데이터
		if (itXml.hasNext()) {
			map = new HashMap();
			Element el = (Element)itXml.next();
			map.put("SugID", el.getChild("SugID").getText());
			map.put("EmpID", el.getChild("EmpID").getText());
			map.put("EmpName", el.getChild("EmpName").getText());
			map.put("CompanyCD", el.getChild("CompanyCD").getText());
			map.put("DeptName", el.getChild("DeptName").getText());
			map.put("TelNum", el.getChild("TelNum").getText());
			map.put("Title", el.getChild("Title").getText());
			map.put("Content", el.getChild("Content").getText().replaceAll("&lt;","<").replaceAll("&gt;",">").replaceAll("&nbsp;"," ").replaceAll("&amp;","&"));
			map.put("CreateDate", el.getChild("CreateDate").getText());
			map.put("KeepFlag", el.getChild("KeepFlag").getText());
			map.put("SugFlag", el.getChild("SugFlag").getText());
			map.put("AcceptDeptCD", el.getChild("AcceptDeptCD").getText());
			map.put("StatusCD", el.getChild("StatusCD").getText());
			map.put("JudgeEmpID", el.getChild("JudgeEmpID").getText());
			map.put("JudgeEmpName", el.getChild("JudgeEmpName").getText());
			map.put("ExecEmpID", el.getChild("ExecEmpID").getText());
			map.put("JudgeExecEmpName", el.getChild("JudgeExecEmpName").getText());
			map.put("JudgeClass", el.getChild("JudgeClass").getText());
			map.put("JudgeResult", el.getChild("JudgeResult").getText());
			map.put("JudgeDate", el.getChild("JudgeDate").getText());
			map.put("ExecSchDate", el.getChild("ExecSchDate").getText());
			map.put("JudgeOpinion", el.getChild("JudgeOpinion").getText());
			map.put("JudgeReason", el.getChild("JudgeReason").getText());
			map.put("AcceptDeptName", el.getChild("AcceptDeptName").getText());
			map.put("IdeaEmpTitleName", el.getChild("IdeaEmpTitleName").getText());
			map.put("SugFlagCD", el.getChild("SugFlagCD").getText());
			map.put("ExecSchFromDate", el.getChild("ExecSchFromDate").getText());
			map.put("JudgeExecEmpTitle", el.getChild("JudgeExecEmpTitle").getText());
			map.put("StrChJudgeDept", el.getChild("StrChJudgeDept").getText());
			if (el.getChild("Unsigned") != null)
			map.put("Unsigned", el.getChild("Unsigned").getText());
		}
		buffer.delete(0, buffer.length());
		buffer.append(url); 
		buffer.append("/oi_suggestion.aspx?strGubun=ExecView&mode=3");
		buffer.append("&Seq=").append(seq);
		buffer.append("&ESeq=").append(eseq);
		buffer.append("&companycd=").append(cpyCD);
		// 채택 실시제안 내용
		doc = ComUtil.getDocument(buffer.toString(), userId, userPw);
		element = doc.getRootElement();
		xmlList = element.getChildren("BoardDetail");
		itXml = xmlList.iterator();
		Map cMap = null;
		String[] strJoinerArr = null;
		// XML 데이터
		if (itXml.hasNext()) {
			cMap = new HashMap();
			Element el = (Element)itXml.next();
			cMap.put("SugID", el.getChild("SugID").getText());
			cMap.put("IdeaBaseSeq", el.getChild("IdeaBaseSeq").getText());
			cMap.put("ThemeSeq", el.getChild("ThemeSeq").getText());
			cMap.put("ExecEmpID", el.getChild("ExecEmpID").getText());
			cMap.put("ExecEmpName", el.getChild("ExecEmpName").getText());
			cMap.put("CompanyCD", el.getChild("CompanyCD").getText());
			cMap.put("ExecEmpDeptCD", el.getChild("ExecEmpDeptCD").getText());
			cMap.put("ExecEmpDeptName", el.getChild("ExecEmpDeptName").getText());
			cMap.put("TelNum", el.getChild("TelNum").getText());
			cMap.put("EmpID", el.getChild("EmpID").getText());
			cMap.put("Title", el.getChild("Title").getText());
			cMap.put("Content", el.getChild("Content").getText().replaceAll("&lt;","<").replaceAll("&gt;",">").replaceAll("&nbsp;"," ").replaceAll("&amp;","&"));
			cMap.put("CreateDate", el.getChild("CreateDate").getText());
			cMap.put("ExecSDate", el.getChild("ExecSDate").getText());
			cMap.put("ExecEDate", el.getChild("ExecEDate").getText());
			cMap.put("KeepFlag", el.getChild("KeepFlag").getText());
			cMap.put("SugFlag", el.getChild("SugFlag").getText());
			cMap.put("SugFlagName", el.getChild("SugFlagName").getText());
			cMap.put("StatusCD", el.getChild("StatusCD").getText());
			cMap.put("Reson", el.getChild("Reson").getText());
			cMap.put("YearEffect", el.getChild("YearEffect").getText());
			cMap.put("ThisYearEffect", el.getChild("ThisYearEffect").getText());
			cMap.put("Status", el.getChild("Status").getText());
			cMap.put("StatusName", el.getChild("StatusName").getText());
			cMap.put("JudgeEmpID", el.getChild("JudgeEmpID").getText());
			cMap.put("JudgeEmpName", el.getChild("JudgeEmpName").getText());
			cMap.put("JudgeClassName", el.getChild("JudgeClassName").getText());
			cMap.put("JudgeResultName", el.getChild("JudgeResultName").getText());
			cMap.put("JudgeDate", el.getChild("JudgeDate").getText());
			cMap.put("JudgeOpinion", el.getChild("JudgeOpinion").getText());
			cMap.put("JudgeReason", el.getChild("JudgeReason").getText());
			cMap.put("ExecEmpTitleName", el.getChild("ExecEmpTitleName").getText());
			cMap.put("Class", el.getChild("Class").getText());
			strJoinerArr = ComUtil.nullChk(el.getChild("strJoiner").getText()).split(";");
			cMap.put("JudgeDeptCD", el.getChild("JudgeDeptCD").getText());
			cMap.put("JudgeDeptName", el.getChild("JudgeDeptName").getText());
			cMap.put("SearchAuth", el.getChild("SearchAuth").getText());
			cMap.put("Subject", el.getChild("Subject").getText());
			cMap.put("JedgeChangCnt", el.getChild("JedgeChangCnt").getText());
			cMap.put("YearEffect2", el.getChild("YearEffect2").getText());
			cMap.put("ThisYearEffect2", el.getChild("ThisYearEffect2").getText());
			if (el.getChild("Unsigned") != null)
			cMap.put("Unsigned", el.getChild("Unsigned").getText());
		}
		String perStr = "";
		if (strJoinerArr != null && !"".equals(strJoinerArr[0])) {
			for (int i=0, len=strJoinerArr.length; i<len; i++) {
				String[] per = strJoinerArr[i].split(",");
				perStr += per[1]+"("+per[2]+") ";
			}
		}
		cMap.put("perStr", perStr);
		// ---[실천과제 목록]-----------------------------------------------------
		buffer.delete(0, buffer.length());
		buffer.append(url);
		buffer.append("/oi_suggestion.aspx?strGubun=CommentList&Mode=view");
		buffer.append("&ExecBasicSeq=").append(seq);
		buffer.append("&companycd=").append(cpyCD);
		doc = ComUtil.getDocument(buffer.toString(), userId, userPw);
		element = doc.getRootElement();
		xmlList = element.getChildren("commentlist");
		itXml = xmlList.iterator();
		List cmtMapList = new ArrayList();
		Map cmtMap = null; 
		// XML 데이터 (목록 정보)
		while (itXml.hasNext()) {
			cmtMap = new HashMap();
			Element el = (Element)itXml.next();
			cmtMap.put("CompanyCD", el.getChild("CompanyCD").getText());
			cmtMap.put("ExecBasicSeq", el.getChild("ExecBasicSeq").getText());
			cmtMap.put("CommentCD", el.getChild("CommentCD").getText());
			cmtMap.put("UserID ", el.getChild("UserID").getText());
			cmtMap.put("UserName", el.getChild("UserName").getText());
			cmtMap.put("StartDate", el.getChild("StartDate").getText());
			cmtMap.put("EndDate", el.getChild("EndDate").getText());
			cmtMap.put("Contents", el.getChild("Contents").getText());
			cmtMap.put("CreateDate", el.getChild("CreateDate").getText());
			cmtMapList.add(cmtMap);
		}
		// 리턴
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("map", map);
		modelAndView.addObject("cMap", cMap);
		modelAndView.addObject("cmtMapList", cmtMapList);
		modelAndView.addObject("page", page);
		return modelAndView;
	}

}
